package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.WeightAndVolumeEntity;
import com.sgb.kjwl.utils.NumberUtil;
import com.sgb.kjwl.view.ui.adapter.transport.WeightAndVolumeMinAndMaxAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.TransportAdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;

import java.util.ArrayList;
import java.util.List;

public class SetWeightAndVolumeWindow extends BasePopWindow {

    private static final float WEIGHT_MIN_VALUE = 0f;
    private static final float WEIGHT_MAX_VALUE = 999.9f;
    private static final float VOLUME_MIN_VALUE = 0f;
    private static final float VOLUME_MAX_VALUE = 999.9f;

    private RecyclerView weightRecyclerView;
    private RecyclerView volumeRecyclerView;
    private EditText weightMin;
    private EditText weightMax;
    private EditText volumeMin;
    private EditText volumeMax;
    private WeightAndVolumeEntity currentWeight;
    private WeightAndVolumeEntity currentVolume;
    private TextView resetButton;
    private TextView sureButton;
    SetTransportWeightWindowListener listener;


    private WeightAndVolumeMinAndMaxAdapter weightAdapter;
    private WeightAndVolumeMinAndMaxAdapter volumeAdapter;

    public interface SetTransportWeightWindowListener {
        void onSureClick(float weightMin, float weightMax, float volumeMin, float volumeMax);
    }

    public SetWeightAndVolumeWindow(Context context, SetTransportWeightWindowListener listener) {
        super(context);
        this.listener = listener;
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_set_transport_weight;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        weightRecyclerView = holder.getPop().findViewById(R.id.weight_recyclerView);
        volumeRecyclerView = holder.getPop().findViewById(R.id.volume_recyclerView);
        weightMin = holder.getPop().findViewById(R.id.weight_min);
        weightMax = holder.getPop().findViewById(R.id.weight_max);
        volumeMin = holder.getPop().findViewById(R.id.volume_min);
        volumeMax = holder.getPop().findViewById(R.id.volume_max);
        resetButton = holder.getPop().findViewById(R.id.tv_re_set);
        sureButton = holder.getPop().findViewById(R.id.tv_ok);

        initWeightRecyclerView();
        initVolumeRecyclerView();
        initWeightMinAndMax();
        initVolumeMinAndMax();
        initResetButton();
        initSureButton();
    }

    private void initSureButton() {
        sureButton.setOnClickListener(v -> {
            if (getVolumeMinValue() > getVolumeMaxValue() || getWeightMinValue() > getWeightMaxValue()) {
                ToastUtil.show(mContext, "最小值不能大于最大值");
                return;
            }
            listener.onSureClick(getWeightMinValue(), getWeightMaxValue(), getVolumeMinValue(), getVolumeMaxValue());
            dismiss();
        });
    }

    private void initResetButton() {
        resetButton.setOnClickListener(view -> {
            currentVolume = null;
            currentWeight = null;
            resetVolumeMinAndMax();
            resetWeightMinAndMax();
            resetVolumeRecycleView();
            resetWeightRecycleView();
        });
    }

    private void initVolumeMinAndMax() {
        volumeMin.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                updateVolumeValueToValidValue(volumeMin, charSequence);
                if (!TextUtils.isEmpty(charSequence)) {
                    resetVolumeRecycleView();
                    currentVolume = null;
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        volumeMax.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                updateVolumeValueToValidValue(volumeMax, charSequence);
                if (!TextUtils.isEmpty(charSequence)) {
                    resetVolumeRecycleView();
                    currentVolume = null;
                }

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    private void resetWeightRecycleView() {
        weightAdapter.resetCheckDatas();
        weightAdapter.setDefItem(-1);
    }

    private void resetVolumeRecycleView() {
        volumeAdapter.resetCheckDatas();
        volumeAdapter.setDefItem(-1);
    }

    private void initWeightMinAndMax() {
        weightMin.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                updateWeightValueToValidValue(weightMin, charSequence);
                if (!TextUtils.isEmpty(charSequence)) {
                    resetWeightRecycleView();
                    currentWeight = null;
                }

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        weightMax.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                updateWeightValueToValidValue(weightMax, charSequence);
                if (!TextUtils.isEmpty(charSequence)) {
                    resetWeightRecycleView();
                    currentWeight = null;
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    private void initVolumeRecyclerView() {
        List<WeightAndVolumeEntity> datas = new ArrayList<>();
        datas.add(new WeightAndVolumeEntity(0, 10));
        datas.add(new WeightAndVolumeEntity(10, 20));
        datas.add(new WeightAndVolumeEntity(20, 30));
        datas.add(new WeightAndVolumeEntity(30, 40));
        datas.add(new WeightAndVolumeEntity(40, 50));
        datas.add(new WeightAndVolumeEntity(50, VOLUME_MAX_VALUE));
        volumeAdapter = new WeightAndVolumeMinAndMaxAdapter(mContext, datas);
        volumeAdapter.setMultiSelect(false);
        volumeAdapter.setItemSelect(true);
        volumeAdapter.setDefItem(-1);
        volumeAdapter.setOnItemClick(new TransportAdapterOnItemClick<WeightAndVolumeEntity>() {
            @Override
            public void onItemClick(WeightAndVolumeEntity weightAndVolumeEntity, int position) {
                resetVolumeMinAndMax();
                currentVolume = datas.get(position);

            }
        });
        volumeRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        volumeRecyclerView.setAdapter(volumeAdapter);
    }

    private void initWeightRecyclerView() {
        List<WeightAndVolumeEntity> datas = new ArrayList<>();
        datas.add(new WeightAndVolumeEntity(0, 5));
        datas.add(new WeightAndVolumeEntity(5, 10));
        datas.add(new WeightAndVolumeEntity(10, 15));
        datas.add(new WeightAndVolumeEntity(15, 20));
        datas.add(new WeightAndVolumeEntity(20, WEIGHT_MAX_VALUE));
        weightAdapter = new WeightAndVolumeMinAndMaxAdapter(mContext, datas);
        weightAdapter.setMultiSelect(false);
        weightAdapter.setItemSelect(true);
        weightAdapter.setOnItemClick(new TransportAdapterOnItemClick<WeightAndVolumeEntity>() {
            @Override
            public void onItemClick(WeightAndVolumeEntity weightAndVolumeEntity, int position) {
                resetWeightMinAndMax();
                currentWeight = datas.get(position);
            }
        });
        weightRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        weightRecyclerView.setAdapter(weightAdapter);
    }

    private void resetWeightMinAndMax() {
        weightMin.setText("");
        weightMax.setText("");
    }

    private void resetVolumeMinAndMax() {
        volumeMin.setText("");
        volumeMax.setText("");
    }


    private void updateWeightValueToValidValue(EditText view, CharSequence charSequence) {
        if (charSequence == null || charSequence.length() == 0) {
            return;
        }
        float value = Float.valueOf(charSequence.toString());
        if (value < WEIGHT_MIN_VALUE) {
            view.setText(0);
            view.setSelection(view.getText().length());
        }
        if (value > WEIGHT_MAX_VALUE) {
            view.setText(String.valueOf(WEIGHT_MAX_VALUE));
            view.setSelection(view.getText().length());
        }
    }


    private float getWeightMinValue() {
        if (!TextUtils.isEmpty(weightMin.getText().toString())) {
            return Float.valueOf(weightMin.getText().toString());
        } else if (currentWeight != null) {
            return currentWeight.getMin();
        } else {
            return WEIGHT_MIN_VALUE;
        }
    }

    private float getWeightMaxValue() {
        if (!TextUtils.isEmpty(weightMax.getText().toString())) {
            return Float.valueOf(weightMax.getText().toString());
        } else if (currentWeight != null) {
            return currentWeight.getMax();
        } else {
            return WEIGHT_MIN_VALUE;
        }
    }

    private void updateVolumeValueToValidValue(EditText view, CharSequence charSequence) {
        if (charSequence == null || charSequence.length() == 0) {
            return;
        }
        float value = Float.valueOf(charSequence.toString());
        if (value < VOLUME_MIN_VALUE) {
            view.setText(0);
            view.setSelection(view.getText().length());
        }
        if (value > VOLUME_MAX_VALUE) {
            view.setText(String.valueOf(VOLUME_MAX_VALUE));
            view.setSelection(view.getText().length());
        }
    }


    private float getVolumeMinValue() {

        if (!TextUtils.isEmpty(volumeMin.getText().toString())) {
            return Float.valueOf(volumeMin.getText().toString());
        } else if (currentVolume != null) {
            return currentVolume.getMin();
        } else {
            return VOLUME_MIN_VALUE;
        }
    }

    private float getVolumeMaxValue() {
        if (!TextUtils.isEmpty(volumeMax.getText().toString())) {
            return Float.valueOf(volumeMax.getText().toString());
        } else if (currentVolume != null) {
            return currentVolume.getMax();
        } else {
            return VOLUME_MIN_VALUE;
        }
    }

    public boolean isUnSetWeightAndVolume() {
        return TextUtils.isEmpty(volumeMax.getText().toString()) &&
                TextUtils.isEmpty(volumeMin.getText().toString()) &&
                TextUtils.isEmpty(weightMax.getText().toString()) &&
                TextUtils.isEmpty(weightMin.getText().toString()) &&
                currentWeight == null && currentVolume == null;
    }

    public String getWeightAndVolumeStr() {
        if (isUnSetWeightAndVolume()) {
            return "";
        }
        String weightStr = "";
        if (currentWeight != null || !TextUtils.isEmpty(weightMin.getText().toString()) || !TextUtils.isEmpty(weightMax.getText().toString())) {
            weightStr = NumberUtil.trim0(getWeightMinValue()) + " - " + NumberUtil.trim0(getWeightMaxValue());
        }
        String volumeStr = "";
        if (currentVolume != null||!TextUtils.isEmpty(volumeMin.getText().toString()) || !TextUtils.isEmpty(volumeMax.getText().toString())) {
            volumeStr = NumberUtil.trim0(getVolumeMinValue()) + " - " + NumberUtil.trim0(getVolumeMaxValue());
        }

        if (!TextUtils.isEmpty(weightStr) && !TextUtils.isEmpty(volumeStr)) {
            return currentWeight + " " + volumeStr;
        } else if (!TextUtils.isEmpty(volumeStr)) {
            return volumeStr;
        } else if (!TextUtils.isEmpty(weightStr)) {
            return weightStr;
        } else {
            return "";
        }
    }

}
